const dbConfig = require("../utils/dbconfig");
const validatePhoneCode = [];//返回验证码json

/**
 * 随机生成验证码
 * @param min
 * @param max
 * @returns {*}
 */
function rand(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}

/**
 * 判断验证码是否重复发送
 * @param phone
 * @returns {boolean}
 */
let sendCodeP = (phone) => {
    for (let item of validatePhoneCode) {
        if (phone === item.phone) {
            return true;
        }
    }
    return false;
}

/**
 * 模拟验证码发送接口
 * @param req
 * @param res
 */
module.exports.sendCode = (req, res) => {
    const phone = req.query.phone;
    if (sendCodeP(phone)) {
        return res.send({code: 400, msg: '验证码已发送！'})
    }
    // const code = rand(1000, 9999);
    const code = Math.floor(Math.random() * (999999 - 100000) + 100000);
    validatePhoneCode.push({
        code: 200,
        msg: '发送成功',
        phone,
        checkCode: code,
    });
    res.send({code: 200, msg: '发送成功', checkCode: code})
}


module.exports.loginByCodeAndPhone = (req, res) => {
    let {phone, code} = req.query;
    //验证该手机号是否发送过验证码
    if (sendCodeP(phone)) {
        return res.send({code: 400, msg: '验证码已发送！'})
    }
}
